#!/usr/bin/env python
# -*- coding: utf-8 -*-
#================================#
# @author: RedSoxFan and NerfSoldier
# @copyright: 2012 RedSoxFan and NerfSoldier
# @license: GNU General Public License v3.0 
#           <http://www.gnu.org/copyleft/gpl.html>
# @summary: Main Template (Plain Text)
#=======================================#

#=======================================#
# Imports
#=======================================#
import os
from PyQt4 import QtCore, QtGui
#=======================================#

class Template(object):
    name = 'Plain Text'
    def __init__(self,darkdev):
        self.darkdev = darkdev
    def show(self,folder):
        fields = {'folder':folder}
        # Create Dialog
        diag = QtGui.QDialog(self.darkdev)
        diag.setWindowTitle("Create File")
        # Create GridLayout
        grid = QtGui.QGridLayout(diag)
        row = 0
        # Add File Name Label
        grid.addWidget(QtGui.QLabel("File Name:"),row,0)
        row+=1
        # Add Class Name LineEdit (Only Accepts Proper Naming Convention)
        fields['name'] = QtGui.QLineEdit()
        grid.addWidget(fields['name'],row,0)
        row+=1
        # Add Ok/Cancel
        box = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok|QtGui.QDialogButtonBox.Cancel)
        grid.addWidget(box,row,0,alignment=QtCore.Qt.AlignRight)
        box.accepted.connect(lambda *a,**k:not(fields["name"].text().isEmpty()) and self.__generate(diag,fields) or QtGui.QMessageBox.warning(diag,'Invalid','Please Enter A File Name'))
        box.rejected.connect(diag.hide)
        # Show Dialog
        if diag.exec_():
            return str(fields['name'].text())
    def __generate(self,diag,fields):
        self.write(os.path.join(fields['folder'],str(fields['name'].text())), "")
        diag.hide()
        diag.accept()
        return True
    def write(self,loc,text):
        with open(loc,"w") as f:
            f.write(text)
            f.flush()